using System;
using ClimbTracker.BusinessLogic.Entities;
using NHibernate;

namespace ClimbTracker.BusinessLogic.Dao
{
    /// <summary>
    /// Summary description for QuickdrawDao.
    /// </summary>
    public class QuickdrawDao : BaseDao
    {
        public QuickdrawDao()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Retrieve a Quickdraw object from the database by its ID
        /// </summary>
        /// <param name="quickdrawID"></param>
        /// <returns></returns>
        public Quickdraw retrieveQuickdrawByID(int quickdrawID)
        {
            Quickdraw quickdraw = null;
            try
            {
                quickdraw = (Quickdraw)currentSession.Load(typeof(Quickdraw), quickdrawID);
            }
            catch (HibernateException ex)
            {
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }

            return quickdraw;

        }

        /// <summary>
        /// Create a new compeition entry in the database from the given Quickdraw object
        /// </summary>
        /// <param name="quickdraw"></param>
        public void createQuickdraw(Quickdraw quickdraw)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Save(quickdraw);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Delete a the database entry of the given Quickdraw object
        /// </summary>
        /// <param name="quickdraw"></param>
        public void deleteQuickdraw(Quickdraw quickdraw)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Delete(quickdraw);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Update the database entry of the given Quickdraw object
        /// </summary>
        /// <param name="quickdraw"></param>
        public void updateQuickdraw(Quickdraw quickdraw)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Update(quickdraw);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }
    }
}
